﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chinaer.Dao.EF
{
    public class ProvinceDAO : Repository<Model.Entities.Model_Province>, IDao.IProvinceDAO
    {
        public override bool Add(Model.Entities.Model_Province entity)
        {
            return base.Add(entity);
        }
        public override bool Delete(Model.Entities.Model_Province entity)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                context.Entry<Model.Entities.Model_Province>(entity).State = System.Data.Entity.EntityState.Modified;
                entity.IsUse = false;
                entity.IsDelete = true;
                return base.Update(entity);
            }
            //return base.Delete(entity);
        }
        public override bool Delete(string id)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                Model.Entities.Model_Province model_Province = context.Provinces.Where(p => p.ID == id && p.IsUse == true && p.IsDelete == false).FirstOrDefault();
                return Delete(model_Province);
            }
        }
        public override int DeleteByIDs(IList<string> listID)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                var queryResult = from p in context.Provinces
                                  where listID.Contains(p.ID)
                                  select p;
                foreach (var item in queryResult)
                {
                    context.Entry<Model.Entities.Model_Province>(item).State = System.Data.Entity.EntityState.Modified;
                    item.IsUse = false;
                    item.IsDelete = true;
                }
                return context.SaveChanges();
            }
        }
        public override bool Exists(string id)
        {
            return GetByID(id) == null ? false : true;
        }
        public override Model.Entities.Model_Province GetByID(string id)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.Provinces.Where(p => p.ID == id && p.IsUse == true && p.IsDelete == false).FirstOrDefault();
            }
        }
        public override IList<Model.Entities.Model_Province> Query(string sql, int pageIndex, out int recordCount, int pageSize, params System.Data.SqlClient.SqlParameter[] sqlParams)
        {
            return base.Query(sql, pageIndex, out recordCount, pageSize, sqlParams);
        }
        public override bool Update(Model.Entities.Model_Province entity)
        {
            return base.Update(entity);
        }

        public List<Model.Entities.Model_Province> GetProvincesByCountryID(string countryID)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.Provinces.Where(p => p.IsDelete == false && p.IsUse == true && p.CountryID.Equals(countryID)).ToList();
            }
        }


        public Model.Entities.Model_Province GetByName(string provinceName)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.Provinces.Where(p => p.IsUse == true && p.IsDelete == false && p.Name.Equals(provinceName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
            }
        }

        public Model.Entities.Model_Province GetByCode(string provinceCode)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.Provinces.Where(p => p.IsUse == true && p.IsDelete == false && p.Code.Equals(provinceCode, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
            }
        }
    }
}
